package com.wc.alorithm_luogu.P5741;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/4/24 14:00
 * @description https://www.luogu.com.cn/problem/P5741
 * 旗鼓相当的对手 - 加强版
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        // 存储所有人的名字 成绩信息
        People[] peoples = new People[n];
        // 存储满足要求的 name 对应的  arraylist
        HashMap<String, ArrayList<String>> ans = new HashMap<>();
        // 存储所有的名字
        String[] names = new String[n];
        // 存储 map 中的name 所对应的arraylist的数量
        int[] nameC = new int[n];

        for (int i = 0; i < n; i++) {
            People people = new People();
            people.name = sc.next();
            people.chinese = Integer.parseInt(sc.next());
            people.math = Integer.parseInt(sc.next());
            people.english = Integer.parseInt(sc.next());
            peoples[i] = people;
            names[i] = people.name;

            for (int j = 0; j < i; j++) {
                // 前面学生的信息做对比
                People prePeople = peoples[j];
                int total = 0;
                // 对比语文分叉
                if (Math.abs(prePeople.chinese - people.chinese) <= 5) {
                    total += prePeople.chinese - people.chinese;
                } else {
                    continue;
                }
                // 对比数学分差
                if (Math.abs(prePeople.math - people.math) <= 5) {
                    total += prePeople.math - people.math;
                } else {
                    continue;
                }
                // 对比英语分差
                if (Math.abs(prePeople.english - people.english) <= 5) {
                    total += prePeople.english - people.english;
                } else {
                    continue;
                }
                // 对比总分差
                if (Math.abs(total) > 10) {
                    continue;
                }
                // 满足要求的name上面的数量+1;
                nameC[j]++;
                // 加入到当前name 的 arraylist当中
                if (ans.containsKey(prePeople.name)) {
                    ans.get(prePeople.name).add(people.name);
                } else {
                    ArrayList<String> nameArray = new ArrayList<>();
                    nameArray.add(people.name);
                    ans.put(prePeople.name, nameArray);
                }
            }

        }
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < n; i++) {
            if (nameC[i] != 0) {
                ArrayList<String> nameArray = ans.get(names[i]);
                for (int k = 0; k < nameC[i]; k++) {
                    str.append(names[i]);
                    str.append(' ');
                    str.append(nameArray.get(k));
                    str.append('\n');
                }
            }
        }
        System.out.print(str);
    }

    static class People {
        public String name;
        public int chinese;
        public int math;
        public int english;
    }
}
